//
//  MSRoomTakeNotesController.m
//  觅尚
//
//  Created by No.8 on 2019/5/15.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSRoomTakeNotesController.h"
#import "MSRoomTakeNoteCell.h"
#import "MSRoomTakeModel.h"
#import "MSRoomTakeDetailsController.h"
#import "MSRoomConsumptionController.h"

#define pageSize 20
@interface MSRoomTakeNotesController ()<UITableViewDelegate,UITableViewDataSource>
//通用导航栏
{
    CustomNavigationBar *naviagtionBar;
    NSInteger page;
    NSInteger count;
    
}
/** <#name#>*/
@property(nonatomic,strong)NSMutableArray *ExamineArray;

/** 表视图*/
@property(nonatomic,strong)UITableView *tableView;

@end

@implementation MSRoomTakeNotesController

/** Nav实现 */
- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
    self.ExamineArray = [NSMutableArray new];
    
    
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
    
}
- (void)MJChiBaoZiHeaderRefresh{
    page = 1;
    [self ReloadData];
}
- (void)MJChiBaoZiFootderRefresh{
    if (count / pageSize < page) {
        [MBManager showBriefAlert:@"没有更多数据了"];
    }else{
        page++;
    }
    [self FootderRefreshReloadData];
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createNavigationBar];
    
    [self CreattableView];
    
    [self CreatView];
    
    PrepareFooter *footder = [PrepareFooter footerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiFootderRefresh)];
    // 马上进入刷新状态
    self.tableView.mj_footer = footder;
}
- (void)CreatView{
    
    UIView * HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 120 * px)];
    HeaderView.backgroundColor = backgroundcolor;
    [self.view addSubview:HeaderView];
    
    UIView * titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 120 * px)];
    titleView.backgroundColor = BackViewColor;
    [HeaderView addSubview:titleView];
    NSArray * titleArray = @[@"日期",@"房间号",@"开房时间"];
    
    CGFloat CommoditytitleWidth = titleView.width / titleArray.count;
    CGFloat CommoditytitleY = 0;
    CGFloat CommoditytitleHeight = titleView.height;
    for (int index = 0; index < titleArray.count; index ++) {
        /** 标题label*/
        UILabel * Commoditytitle = [[UILabel alloc] init];
        Commoditytitle.x = CommoditytitleWidth * index;
        Commoditytitle.y = CommoditytitleY;
        Commoditytitle.width = CommoditytitleWidth;
        Commoditytitle.height = CommoditytitleHeight;
        Commoditytitle.textColor = WYCRGBColor(102, 121, 145);
        Commoditytitle.text = titleArray[index];
        Commoditytitle.textAlignment = NSTextAlignmentCenter;
        Commoditytitle.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
        [titleView addSubview:Commoditytitle];
    }
    self.tableView.tableHeaderView = HeaderView;
}
- (void)ReloadData{
    [MBManager showLoading];
    [self.ExamineArray removeAllObjects];
    NSDictionary *params = @{@"uid":[WYCAccountTool unarchiveuid],@"page":@(page),@"pageSize":@(pageSize)};
    [HTTPRequest POST:MSgetManagerRoomList params:params succeed:^(id responseObject) {
        if (CodeStr) {
            WYCLog(@"--------%@",responseObject);
            count = [responseObject[@"data"][@"count"] integerValue];
            [self.tableView.mj_header endRefreshing];
            [MBManager hideAlert];
            NSMutableArray *temporaryArray = [NSMutableArray new];
            temporaryArray = [MSRoomTakeModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"roomRecordList"]];
            self.ExamineArray = temporaryArray;
        }else{
            [self.tableView.mj_header endRefreshing];
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page = [params[@"page"] integerValue];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [MBManager hideAlert];
        [MBManager showBriefAlert:Failed];
    }];


}
- (void)FootderRefreshReloadData{
    [MBManager showLoading];
    [self.ExamineArray removeAllObjects];
    NSDictionary *params = @{@"uid":[WYCAccountTool unarchiveuid],@"page":@(page),@"pageSize":@(pageSize)};
    [HTTPRequest POST:MSgetManagerRoomList params:params succeed:^(id responseObject) {
        if (CodeStr) {
            WYCLog(@"--------%@",responseObject);
            [self.tableView.mj_footer endRefreshing];
            [MBManager hideAlert];
            self.ExamineArray = [MSRoomTakeModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"roomRecordList"]];
        }else{
            [self.tableView.mj_footer endRefreshing];
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page --;
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [MBManager hideAlert];
        [MBManager showBriefAlert:Failed];
    }];
    
}
/** 表视图*/
- (void)CreattableView{
    self.extendedLayoutIncludesOpaqueBars = YES;
    //    if (!_tableView) {
    
    if (@available(iOS 11.0, *)) {
        
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
    } else {
        
        self.automaticallyAdjustsScrollViewInsets = NO;
        
    }
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, [self navBarBottom], ScreenWidth, ScreenHeight - [self navBarBottom]) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    //    }
    //    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.ExamineArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MSRoomTakeNoteCell * cell = [MSRoomTakeNoteCell cellWithTableView:tableView];
    tableView.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.ExamineArray.count > indexPath.row) {
        cell.RoomTakeModel = self.ExamineArray[indexPath.row];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MSRoomTakeModel * RoomManageMentModel = self.ExamineArray[indexPath.row];
    MSRoomConsumptionController * RommVC = [[MSRoomConsumptionController alloc] init];
//    RommVC.roomID = RoomManageMentModel.;
    RommVC.Tagstr = @"1";
    RommVC.openid = RoomManageMentModel.openid;
    RommVC.opentime = RoomManageMentModel.opentime;
    WYCLog(@"======%@",RommVC.openid);
    [self.navigationController pushViewController:RommVC animated:NO];
}
- (void)createNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"房间服务记录";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}

@end
